﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AutoUpdateService.Mgr
{
    using AutoUpdateService.Mgr.Model;
    internal class PacketMgr
    {
        #region Instance

        public static PacketMgr instance { set; get; } = null;

        public static PacketMgr getInstance()
        {
            if (instance == null)
            {
                instance = new PacketMgr();
            }

            return instance;
        }

        #endregion


        public void ProcessPacket(ClientSession session, string packetString)
        {
            try
            {
                AutoUpdate.Model.Packet.Packet packet = this.getPacket(packetString);

                packet.clientSessionID = session.channelHashCode;

                HandlerMgr.getInstance().doAction(packet);

                return;


                if (packet.CommandText.ToUpper() == "REGISTE")
                {
                    ClientMgr.getInstance().registeClient(session.channelHashCode, packet.Topic);
                }
                else if (packet.CommandText.ToUpper() == "REREGISTE")
                {
                    ClientMgr.getInstance().registeClient(session.channelHashCode, packet.Topic);
                }
                else if (packet.CommandText.ToUpper() == "HEARTBEAT")
                {
                    //ClientMgr.getInstance().registeClient(session.channelHashCode, packet.Topic);
                }
                else
                {
                    switch (packet.Topic)
                    {

                        case "SERVER":

                            packet.clientSessionID = session.channelHashCode;

                            HandlerMgr.getInstance().doAction(packet);

                            break;

                        default:

                            if (packet.PacketType == AutoUpdate.Model.Packet.PacketType.Request)

                                packet.clientSessionID = session.channelHashCode;

                            Console.WriteLine(session.channelHashCode);
                            Console.WriteLine(packet.clientSessionID);
                            ClientMgr.getInstance().transferMessage(packet);

                            break;
                    }



                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public AutoUpdate.Model.Packet.Packet getPacket(string msg)
        {
            AutoUpdate.Model.Packet.Packet result = null;

            try
            {

                result = Newtonsoft.Json.JsonConvert.DeserializeObject<AutoUpdate.Model.Packet.Packet>(msg);

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        public string getPacketString(AutoUpdate.Model.Packet.Packet packet)
        {
            string result = "";

            result = Newtonsoft.Json.JsonConvert.SerializeObject(packet);

            return result;
        }
    }
}
